package com.qk.controller;

import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.qk.common.Result;
import com.qk.enums.ApiEnum;
import com.qk.exception.CommonException;
import com.qk.util.HttpsApiClientWeather;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/09 11:30
 * @Description: 天气接口
 */
@RestController
@Slf4j
@RequestMapping("/weather")
@SuppressWarnings("all")
public class weatherHandleController {

    @Autowired
    public HttpsApiClientWeather httpsApiClientWeather;

    /**
     * 通过GPS查询天气预报
     *
     * @param from              输入的坐标类型：
     *                          <p>1：GPS设备获取的角度坐标 默认</p>
     *                          <p>2：GPS获取的米制坐标、sogou地图所用坐标</p>
     *                          <p>3：google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标</p>
     *                          <p>4：3中列表地图坐标对应的米制坐标</p>
     *                          <p>5：百度地图采用的经纬度坐标</p>
     *                          <p>6：百度地图采用的米制坐标</p>
     *                          <p>7：mapbar地图坐标</p>
     *                          <p>8：51地图坐标</p>
     * @param lng               经度值 必须
     * @param lat               纬度值 必须
     * @param needMoreDay       是否需要返回7天数据中的后4天。1为返回，0为不返回
     * @param needIndex         是否需要返回指数数据，比如穿衣指数、紫外线指数等。1为返回，0为不返回
     * @param needHourData      是否需要每小时数据的累积数组。由于本系统是半小时刷一次实时状态，因此实时数组最大长度为48。每天0点长度初始化为0. 1为需要 0为不
     * @param need3HourForecast 是否需要当天每3/6小时一次的天气预报列表。1为需要，0为不需要
     * @param needAlarm         是否需要天气预警。1为需要，0为不需要
     * @return String
     */
    @GetMapping("/gps")
    public Result gps(@RequestParam(defaultValue = "1", required = false) String from,
                      String lng, String lat,
                      @RequestParam(defaultValue = "0", required = false) String needMoreDay,
                      @RequestParam(defaultValue = "0", required = false) String needIndex,
                      @RequestParam(defaultValue = "0", required = false) String needHourData,
                      @RequestParam(defaultValue = "0", required = false) String need3HourForcast,
                      @RequestParam(defaultValue = "0", required = false) String needAlarm) {
        log.info("lng: {}, lat: {},", lng, lat);
        httpsApiClientWeather.init(HttpsApiClientWeather.HTTPS);
        ApiResponse apiResponse = httpsApiClientWeather.forecastByGPSSyncMode(from, lng, lat, needMoreDay, needIndex, needHourData, need3HourForcast, needAlarm);
        log.info("apiResponse: {}", apiResponse);
        try {
            String resultString = httpsApiClientWeather.getResultString(apiResponse);
            log.info("resultString: {}", resultString);
            return Result.success(resultString);
        } catch (IOException e) {
            log.error("获取天气失败：{}", e.getMessage());
            CommonException.throwException(ApiEnum.WEATHER_ERROR);
            return null;
        }
    }
}
